cmake_minimum_required(VERSION 3.20 FATAL_ERROR)

project(freemodbus
    VERSION 1.0.0
    LANGUAGES C CXX
    DESCRIPTION "FreeModbus is a free and open source Modbus implementation"
    HOMEPAGE_URL "https://gitee.com/exp-open/freemodbus"
)

add_library(freemodbus STATIC)

option(FREEMODBUS_WITH_ASCII_FRAMING "Enable ASCII framing support" ON)
option(FREEMODBUS_WITH_RTU_FRAMING "Enable RTU framing support" ON)
option(FREEMODBUS_WITH_TCP_FRAMING "Enable TCP framing support" OFF)
option(FREEMODBUS_WITH_ASCII_MASTER "Enable ASCII master support" OFF)
option(FREEMODBUS_WITH_RTU_MASTER "Enable RTU master support" OFF)
option(FREEMODBUS_WITH_TCP_MASTER "Enable TCP master support" OFF)
option(FREEMODBUS_WITH_ASCII_SLAVE "Enable ASCII slave support" OFF)
option(FREEMODBUS_WITH_RTU_SLAVE "Enable RTU slave support" ON)
option(FREEMODBUS_WITH_TCP_SLAVE "Enable TCP slave support" OFF)
option(FREEMODBUS_WITH_FUNCTION "Enable function support" ON)
option(FREEMODBUS_WITH_PORT_WINDOWS "Enable port support For Windows" ON)

file(GLOB SOURCES modbus/*.c)

if(FREEMODBUS_WITH_PORT_WINDOWS)
        list(APPEND SOURCES 
        demo/WIN32/port/portevent.c 
        demo/WIN32/port/portserial.c 
        demo/WIN32/port/porttimer.c
        demo/WIN32/port/portother.c
        )
        target_include_directories(${PROJECT_NAME} PUBLIC demo/WIN32/port)
endif(FREEMODBUS_WITH_PORT_WINDOWS)

if(FREEMODBUS_WITH_ASCII_FRAMING)
    list(APPEND SOURCES modbus/ascii/mbascii.c)
    target_compile_definitions(${PROJECT_NAME} PUBLIC MB_ASCII_ENABLED=1)
    target_include_directories(${PROJECT_NAME} PUBLIC modbus/ascii)
endif(FREEMODBUS_WITH_ASCII_FRAMING)

if(FREEMODBUS_WITH_RTU_FRAMING)
    list(APPEND SOURCES modbus/rtu/mbrtu.c modbus/rtu/mbcrc.c)
    target_compile_definitions(${PROJECT_NAME} PUBLIC MB_RTU_ENABLED=1)
    target_include_directories(${PROJECT_NAME} PUBLIC modbus/rtu)
endif(FREEMODBUS_WITH_RTU_FRAMING)

if(FREEMODBUS_WITH_TCP_FRAMING)
    list(APPEND SOURCES modbus/tcp/mbtcp.c)
    target_compile_definitions(${PROJECT_NAME} PUBLIC MB_TCP_ENABLED=1)
    target_include_directories(${PROJECT_NAME} PUBLIC modbus/tcp)
endif(FREEMODBUS_WITH_TCP_FRAMING)

if(FREEMODBUS_WITH_FUNCTION)
    list(APPEND SOURCES
    modbus/functions/mbfunccoils.c
    modbus/functions/mbfuncdiag.c
    modbus/functions/mbfuncdisc.c
    modbus/functions/mbfuncholding.c
    modbus/functions/mbfuncinput.c
    modbus/functions/mbfuncother.c
    modbus/functions/mbutils.c
    )
endif(FREEMODBUS_WITH_FUNCTION)

target_sources(${PROJECT_NAME} PUBLIC ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC modbus/include)

if(FREEMODBUS_WITH_PORT_WINDOWS)
    add_executable(app demo/WIN32/demo.cpp)
    target_link_libraries(app freemodbus)
endif(FREEMODBUS_WITH_PORT_WINDOWS)